import { ProColumns, ProTable } from '@ant-design/pro-table';
import { message, Modal } from 'antd';
import React, { PropsWithChildren } from 'react';
import { updateCureTeam } from '@/services/teamService';
import { TeamType } from '@/models/team';
import { UserType } from '@/models/user';

interface UpdateCureModalProps {
  oldData: TeamType.TeamVo;
  modalVisible: boolean;
  onSubmit: () => void;
  onCancel: () => void;
  loginUser: UserType.UserVO;
}

/**
 *  更新数据模型框
 * @param fields
 */
const updateCure = async (fields: TeamType.TeamUpdateCureRequest) => {
  if ((fields?.userId || 0) <= 0) {
    message.loading('请先进行登入！');
    return [];
  }
  const hide = message.loading('提交修改申请');
  try {
    await updateCureTeam({
      ...fields,
      userId: fields.userId,
      teamId: fields.teamId,
    });
    hide();
    message.success('发送申请成功');
    return true;
  } catch (e: any) {
    hide();
    message.error('申请失败:' + e.message);
    return false;
  }
};

/**
 *  更新数据模型框
 * @param props
 */
const UpdateCureModal: React.FC<PropsWithChildren<UpdateCureModalProps>> = (
  props,
) => {
  const { oldData, modalVisible, onSubmit, onCancel, loginUser } = props;

  /***
   * 表格列配置
   */
  const columns: ProColumns<TeamType.Team>[] = [
    {
      title: '队伍的名称',
      dataIndex: 'teamName',
      valueType: 'text',
    },
    {
      title: '队伍的描述',
      dataIndex: 'teamDescription',
      valueType: 'textarea',
    },
    {
      title: '队伍图片（远程地址）',
      dataIndex: 'teamAvatarUrl',
      valueType: 'image',
      ellipsis: true,
    },
  ];

  return (
    <Modal
      destroyOnClose
      title="更新"
      open={modalVisible}
      onCancel={() => onCancel()}
      footer={null}
    >
      <ProTable<TeamType.Team, TeamType.Team>
        rowKey="id"
        type="form"
        columns={columns}
        form={{
          initialValues: oldData,
        }}
        onSubmit={async (value) => {
          const success = await updateCure({
            ...value,
            teamId: oldData.id,

            userId: loginUser?.id,
          });
          if (success) {
            onSubmit?.();
          }
        }}
      />
    </Modal>
  );
};

export default UpdateCureModal;
